package wli.java.file.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.net.URI;
import java.net.URISyntaxException;

public class FileDirectoryToHtmlList {

	private String directory;
	private List<FileStructure> list;
	private List<FileStructure> list2;
	private URI directory_uri;

	public void setDirectory(String dir) {
		this.directory = dir;
	}

	public List<FileStructure> getDirectoryList2(File[] files) {
		list2 = new ArrayList<FileStructure>();
		File f = new File(directory);
	    files = f.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				System.out.println("Directory: " + file.getName()+" Path="+file.getPath());
				getDirectoryList2(file.listFiles()); 
			} else {
				//System.out.println("File: " + file.getName());
			}
		}
		return list2;
	}
	
	/*
	public static void showFiles(File[] files) {
		for (File file : files) {
			if (file.isDirectory()) {
				System.out.println("Directory: " + file.getName()+" Path="+file.getPath());
				showFiles(file.listFiles()); // Calls same method again.
			} else {
				//System.out.println("File: " + file.getName());
			}
		}
	}
*/
	public List<FileStructure> getDirectoryList() throws URISyntaxException {
		list = new ArrayList<FileStructure>();

		File f = new File(directory);
		Vector<File> v = new Vector<File>();
		v.add(f);

		String[] flist;
		File tf;

		Enumeration<File> enums = v.elements();
		int nn = 0;
		int mm = 0;
		FileStructure fstu = null;
		boolean add_one = false;

		while (enums.hasMoreElements()) {
			add_one = false;

			fstu = new FileStructure();
			f = enums.nextElement();

			if (f != null && f.isDirectory()) {

				flist = f.list();
				for (int n = 0; n < flist.length; n++) {
					nn++;
					tf = new File(f.getPath(), flist[n]);
					if (tf.isDirectory()) {
						v.add(new File(tf.getPath()));
						fstu = new FileStructure();
						fstu.setId(nn);
						fstu.setPid(mm);
						fstu.setPath(tf.getParent());
						fstu.setIsFile(false);
						fstu.setName(tf.getName());
						fstu.setUrl(tf.toURI());
						if (directory.equals(tf.getParent())) {
							fstu.setDisplyOn(true);
						}
						add_one = true;
						list.add(fstu);
					}
				}
			} else {
				fstu = new FileStructure();
				fstu.setId(nn);
				fstu.setPid(mm);
				fstu.setPath(f.getParent());
				fstu.setIsFile(true);
				fstu.setName(f.getName());
				fstu.setUrl(f.toURI());
				fstu.setDisplyOn(false);
				add_one = true;
				list.add(fstu);
			}

			if (add_one) {
				System.out.println(">>>>>>>>>>>>>>>>>>>" + mm
						+ ">>>>>>>>>>>>>>>" + fstu.getPath());
			}
		}

		return list;
	}
}